﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Radiation___IM;

namespace RadiationUI
{
    public partial class ChannelNameDialog : Form
    {
        private Form m_parent;
        public ChannelNameDialog(Form parent)
        {
            InitializeComponent();
            m_parent = parent;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!this.textBox1.Text.Contains(' ') &&
                !this.textBox1.Text.Contains(';') &&
                !this.textBox1.Text.Contains(']') &&
                !this.textBox1.Text.Contains('\"'))
            {
                if (WindowManager.Instance.addChannel(this.textBox1.Text, m_parent))
                {
                    WindowManager.Instance[this.textBox1.Text].Show();
                    /*byte[] message = UnicodeEncoding.Unicode.GetBytes(AESManager.Instance.Encrypt("[channel=default;c=0]/api channels.join_or_create " + textBox1.Text));
                    Connection.Client.Client.BeginSend(message, 0, message.Length, System.Net.Sockets.SocketFlags.None, endSend, this);*/
                    Connection.sendAsync("[channel=default;c=0]/api channels.join_or_create " + textBox1.Text);
                    this.Close();
                }
                else
                    MessageBox.Show("Impossible d'ouvrir le canal car il est déjà ouvert.", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
                MessageBox.Show("Le nom du canal contiens des caractères invalides", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        /*private void endSend(IAsyncResult result)
        {
            Connection.Client.Client.EndSend(result);
        }*/

        private void ChannelNameDialog_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                this.Close();
            if (e.KeyCode == Keys.Enter && textBox1.Focused)
                button1.Focus();
        }
    }
}
